package com.ftg.learn.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.ftg.learn.common.dto.MemberDto;
import com.ftg.learn.common.util.R;
import com.ftg.learn.common.vo.MemberVo;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.ftg.learn.entity.Education;
import com.ftg.learn.entity.Member;
import com.ftg.learn.mapper.EducationMapper;
import com.ftg.learn.mapper.MemberMapper;
import com.ftg.learn.service.MemberService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author 李孝辉
 * @Date 2025/6/16 10:28
 * @email 1626285873@qq.com
 * @Phone 18724505266
 * @内容
 */
@Service
public class MemberServiceImpl implements MemberService {

    @Resource
    private MemberMapper memberMapper;

    @Resource
    private EducationMapper educationMapper;

    @Override
    public List<MemberVo> getAllMember() {
        return memberMapper.showAll();
    }

    @Override
    public int addMember(MemberDto memberDto) {
        return memberMapper.addMember(memberDto);
    }

    @Override
    public int updateMember(MemberDto memberDto) {
        return memberMapper.updateMember(memberDto);
    }

    @Override
    public int deleteMember(Long id) {
        return memberMapper.deleteMember(id);
    }

    @Override
    public R<Integer> TimeIntegral(int time, Long id) {
        int integral = time / 60;//将传入的时间转换为积分(每一分钟为一个积分)
        int result = memberMapper.TimeIntegral(integral, id); //mapper //将积分存储到数据库
        if (result == 0) {
            System.out.println("积分存储失败");
            return R.error("积分存储失败");
        }
        return R.success(integral);//返回积分
    }

    /**
     * @Author yuan
     * @Description 存储学习经验 返回值第一个值为当前学习等级 第二个值为当前学习等级余下经验
     * @Date 13:02 2025/6/19
     * @Param [topic, userID]
     * @Return List<Integer>
     **/
    @Override
    public R<List<Integer>> answerPoints(int topic, int userID) {
        int result = memberMapper.updateEducationThreshold(topic, userID);  // 存储学习经验
        if (result == 0) {
            System.out.println("学习经验存储失败");
            return R.error("学习经验存储失败");
        }

        List<Education> educations = educationMapper.findAll(); // 查找所有学习等级和经验
        Member me = memberMapper.getMemberById(userID); // 获取当前用户的学习经验

        Long threshold = 0L; // 当前等级经验
        // 计算学习等级和剩余经验
        for (int i = 0; i < educations.size(); i++) {
            if (educations.get(educations.size() - 1).getThreshold() < threshold) {
                threshold = educations.get(educations.size() - 1).getThreshold();
                if (educations.get(educations.size() - 1).getId() == me.getEducationId()) {
                    break;
                } else {
                    me.setEducationId(educations.get(educations.size() - 1).getId());
                    memberMapper.updateEducationID(me.getEducationId(), userID);
                    break;
                }
            }

            if (educations.get(i).getThreshold() < me.getEducationThreshold() &&
                    educations.get(i + 1).getThreshold() >= me.getEducationThreshold()) {
                threshold = educations.get(i).getThreshold();
                me.setEducationId(educations.get(i).getId() + 1);
                memberMapper.updateEducationID(me.getEducationId(), userID);
                break;
            }
        }

        ArrayList<Integer> list = new ArrayList<>();
        list.add(me.getEducationId());
        list.add((int) (me.getEducationThreshold() - threshold));

        return R.success(list);
    }

    @Override
    public void SetThreshold(String query) {
        //query为整段的支付信息,包括金额
        JSONObject jsonObject = JSONObject.parseObject(query);
        Object o = jsonObject.get("alipay_trade_query_response");
        System.out.println("o==>" + o);
        Map map = (Map) o;
        String totalAmount = map.get("total_amount").toString();//拿到其中的金额,将其转换为字符串
        float threshold = Float.parseFloat(totalAmount);//将金额转换为float
        Long VipGradeThreshold = (long) (threshold);//vip等级
        Long EducationThreshold = (long) (threshold * 2);//学习等级
        Long id = 10L;//测试调用,后续用token获取id
        int result = memberMapper.LearnIntegral(VipGradeThreshold, EducationThreshold, id);
        if (result == 0) {
            System.out.println("会员等级积分增加失败: " + id);
        }
        System.out.println("会员等级积分增加成功: " + id);
    }

}
